#include "httpget.h"

HttpGet::HttpGet(QObject *parent) :
    QObject(parent),
    manager(NULL),
    reply(NULL)
{
    manager = new QNetworkAccessManager(this);
    connect(manager, SIGNAL(finished(QNetworkReply*)),
            manager, SLOT(deleteLater()));
}

void HttpGet::doConnect()
{
    if (manager != NULL){
        QUrl url = QUrl("http://fanyi.youdao.com/openapi.do?keyfrom=HelloQt&key=1824600459&type=data&doctype=json&version=1.1&q=文本");
        reply = manager->get(QNetworkRequest(url));
        if (reply != NULL){
            connect(reply, SIGNAL(readyRead()), this, SLOT(readyRead()));
            connect(reply, SIGNAL(finished()), reply, SLOT(deleteLater()));
        }
    }
}

void HttpGet::readyRead()
{
    if (reply != NULL){
        result = reply->readAll();
    }
    qDebug() << result;
    emit finished();
}

QByteArray HttpGet::getResult() const
{
    return result;
}
